package com.huazi.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huazi.commonutils.CommentResult;
import com.huazi.statistics.client.UcenterClient;
import com.huazi.statistics.entity.Daily;
import com.huazi.statistics.mapper.DailyMapper;
import com.huazi.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author huazi
 * @since 2021-04-01
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {
    @Autowired
    UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) {
        CommentResult cr = ucenterClient.countRegister(day);
        //获取数据
        Integer count = (Integer) cr.getData().get("count");
        //添加数据前先判断是否已经存在当前日期统计
        Daily daily = baseMapper.selectOne(new QueryWrapper<Daily>().eq("date_calculated", day));
        if(daily==null){//可以使用删除法
            daily=new Daily();
            //将这些数据存入统计分析表里面
            daily.setRegisterNum(count);//注册人数
            daily.setDateCalculated(day);//统计日期
            daily.setVideoViewNum(RandomUtils.nextInt(100,200));//100到200之间数
            daily.setCourseNum(RandomUtils.nextInt(100,200));//课程数
            daily.setLoginNum(RandomUtils.nextInt(100,200));
            baseMapper.insert(daily);
            return;
        }
        //存在就进行修改
        daily.setRegisterNum(count);
        baseMapper.updateById(daily);
    }
    //根据type类型获取相应数据
    @Override
    public Map<String, Object> getSearchData(String type, String begin, String end) {
        //条件查询对象
        QueryWrapper<Daily> wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end)
                .select("date_calculated",type).orderByAsc("date_calculated");
        List<Daily> dailies = baseMapper.selectList(wrapper);
        //因为返回有两部分数据:日期和日期对应数量
        //前端要求数组json结构，对应后端java代码是list集合
        //创建两个list集合，一个日期list,一个数量list
        List<String> date_calculatedList=new CopyOnWriteArrayList<>();
        List<Integer> numDataList=new CopyOnWriteArrayList<>();
        //遍历查询所有数据，分别存入两个list集合中
        Map<String,Object> map=new ConcurrentHashMap<>();
        if("register_num".equals(type)){
            getRegisterNum(dailies,date_calculatedList,numDataList,map);
        }else if("login_num".equals(type)){
            getLoginNum(dailies,date_calculatedList,numDataList,map);
        }else if("course_num".equals(type)){
            getCourseNum(dailies,date_calculatedList,numDataList,map);
        }else if("video_view_num".equals(type)){
            getVideoViewNum(dailies,date_calculatedList,numDataList,map);
        }
        return map;//返回封装好的两个list集合map
    }

    /**
     *
     * @param dailies 数据源
     * @param dateData 日期（x）
     * @param numData 数据（系列内容）
     * @param map 封装两个list集合
     * @return
     */
    public Map<String,Object> getRegisterNum(List<Daily> dailies,List<String> dateData,List<Integer> numData,Map<String,Object> map){
        for (Daily da:dailies
        ) {
            dateData.add(da.getDateCalculated());
            numData.add(da.getRegisterNum());
        }
        map.put("date_calculatedList",dateData);
        map.put("numDataList",numData);
        return map;
    }
    public Map<String,Object> getCourseNum(List<Daily> dailies,List<String> dateData,List<Integer> numData,Map<String,Object> map){

        for (Daily da:dailies
        ) {
            dateData.add(da.getDateCalculated());
            numData.add(da.getRegisterNum());
        }
        map.put("date_calculatedList",dateData);
        map.put("numDataList",numData);
        return map;
    }
    public Map<String,Object> getVideoViewNum(List<Daily> dailies,List<String> dateData,List<Integer> numData,Map<String,Object> map){
        for (Daily da:dailies
        ) {
            dateData.add(da.getDateCalculated());
            numData.add(da.getRegisterNum());
        }
        map.put("date_calculatedList",dateData);
        map.put("numDataList",numData);
        return map;
    }
    public Map<String,Object>  getLoginNum(List<Daily> dailies,List<String> dateData,List<Integer> numData,Map<String,Object> map){
        for (Daily da:dailies
        ) {
            dateData.add(da.getDateCalculated());
            numData.add(da.getRegisterNum());
        }
        map.put("date_calculatedList",dateData);
        map.put("numDataList",numData);
        return map;
    }
}
